 <?php
/* 
	登录页
 */

 class Login extends WL_controller{

	public function __construct(){
		parent::__construct();
	}
	
	/***********************************
		登录页
	 */
	public function index(){

		$this->session->sess_destroy();
		$this->load->view( 'login', $this->data);
	}
	
	/**
	 * 用户登录ajax-post提交验证
	 * @param uid
	 * @param password
	 *
	 */
	public function login_check(){

		$post = $this->input->post( null, true );
		$json = array(
					'status'=>'error',
					'error'=>'未知错误',
		);
		if( ! $this->data['site']->is_open ){
			$json[ 'error' ] = '系统关闭，禁止访问';
			echo json_encode( $json ); return FALSE;
		}
		if( empty( $post['sid'] ) ){
			$json['error'] = '请填写学号';
			echo json_encode( $json ); return FALSE;
		}

		if( empty( $post['sname' ] ) ){
			$json['error'] = '请填写姓名';
			echo json_encode( $json ); return FALSE;
		}


		if( $user = $this->exam_model->get_user_by_sid( $post['sid'] ) ){
			if( $user['sname'] === $post['sname'] ){
				$json[ 'status' ] = 'success';
				$json[ 'error' ] = '登录成功';
				$class = $this->exam_model->get_class_by_cid( $user['cid'] );
				
				//写入session
				set_sess( 'sid', $user['sid'] );
				set_sess( 'sname', $user['sname'] );
				set_sess( 'cname', $class['cname'] );
				set_sess( 'cid', $class['cid'] );
							
			}else{
				$json[ 'error' ] = '学号 与 姓名 不一致！';
			}
		}else{
			$json[ 'error' ] = '没有该考生信息！';
		}

		echo json_encode( $json ); return FALSE;
	}	


	/**
		注销用户
	 */
	public function logout(){
		
		$this->session->sess_destroy();	//注销session
		redirect("/login");
	}

}

?>